//-------------子窗口进行ajax操作get post服务器数据
var host="http://118.25.210.158:1234/"; //做网络验证的服务器地址，可以是域名或者ip

function isjson(obj){ //判断一个变量对象是否为json。内部变量单双引号都行
    if(typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length){
        return true;
    }
    else{
        return false;
    }
}
function str2obj(str){
    var obj=eval('(' + str + ')'); //字符串类型转内部类型 如"[1,2,3]" 转换得到[1,2,3]
    return obj;
}
function isJSON(str) { //判断字符串是否为json格式。内部变量只能双引号，不能单引号
    if (typeof str == 'string') {
        try {
            //var obj=str.substring(1, str.length - 1);
            obj=str2obj(str);
            console.log(obj);
            if(isjson(obj)){
                return true;
            }
            else{
                return false;
            }
        } catch(e) {
            console.log(e);
            return false;
        }
    }
    return false;
}


function ajax_get(filename,json,func,mode){ //最后一个参数为默认，默认是传入值，设置为1传id

//json={"name":"login_name","pwd":"login_pwd"};           此参数一般长这样    
if(!isjson(json)){
    console.log('参数传入错误，类型非json');
    return
}
var xmlhttp;
var str="";
var data="";
if(arguments[3]==true){
for(var key in json){ //遍历json里面的键
     //console.log(key+"--"+document.getElementById(json[key]).value);
     //有空值但是不含type 或者有空值，但是有type,且类型不属于那么几个
     if((document.getElementById(json[key]).value=="" && !json.hasOwnProperty("type"))||(document.getElementById(json[key]).value==""&&json.hasOwnProperty("type")&&json["type"]!="秘境"&&json["type"]!="地图"&&json["type"].indexOf('查询')==-1)){
        alert("操作失败，传入参数有空值！");
        return
    }
    str = key+"="+document.getElementById(json[key]).value;
    data += str+"&";   
}
}
else{
for(var key in json){ //遍历json里面的键
     //console.log(key+"--"+json[key]);
     if((json[key]=="" && !json.hasOwnProperty("type"))||(json[key]=="" &&json.hasOwnProperty("type")&&json["type"]!="秘境"&&json["type"]!="地图"&&json["type"].indexOf('查询')==-1)){
        alert("操作失败，传入参数有空值！");
        return
    }
    str = key+"="+json[key];
    data += str+"&";   
}     
}
data = data.substring(0, data.length - 1);

if (window.XMLHttpRequest)
{
    //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
    xmlhttp=new XMLHttpRequest();
}
else
{
    // IE6, IE5 浏览器执行代码
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        //document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        var ret=xmlhttp.responseText;
        func(ret);
    }
}
    xmlhttp.open("GET",host+filename+"?t="+(new Date().getTime())+"&"+data,true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send();
}

function ajax_post(filename,json,func,mode){ //最后一个参数为默认，默认是传入值，设置为1传id

//json={"name":"login_name","pwd":"login_pwd"};           此参数一般长这样    
if(!isjson(json)){
    console.log('参数传入错误，类型非json');
    return
}
var xmlhttp;
var str="";
var data="";
if(arguments[3]==true){
for(var key in json){ //遍历json里面的键
    //console.log(key+"--"+document.getElementById(json[key]).value);
    if((document.getElementById(json[key]).value=="" && !json.hasOwnProperty("type"))||(document.getElementById(json[key]).value==""&&json.hasOwnProperty("type")&&json["type"]!="秘境"&&json["type"]!="地图"&&json["type"].indexOf('查询')==-1)){
        alert("操作失败，传入参数有空值！");
        return
    }
    str = key+"="+document.getElementById(json[key]).value;
    data += str+"&";   
}
}
else{
for(var key in json){ //遍历json里面的键
     //console.log(key+"--"+json[key]);
     if((json[key]=="" && !json.hasOwnProperty("type"))||(json[key]=="" &&json.hasOwnProperty("type")&&json["type"]!="秘境"&&json["type"]!="地图"&&json["type"].indexOf('查询')==-1)){
        alert("操作失败，传入参数有空值！");
        return
    }
    str = key+"="+json[key];
    data += str+"&";   
}     
}
data = data.substring(0, data.length - 1);

if (window.XMLHttpRequest)
{
    //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
    xmlhttp=new XMLHttpRequest();
}
else
{
    // IE6, IE5 浏览器执行代码
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        //document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        var ret=xmlhttp.responseText;
        func(ret);
    }
}
    xmlhttp.open("POST",host+filename+"?t="+Math.random()+"&",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send(data);
}

function test(ret){
    console.log(ret);
    var str = $('textarea#log').val(); //先获取原有的值
    $('textarea#log').val(str + '\n'+ret); //拼接新值
}

function search_ret(ret){
    //console.log("ret="+ret);
    document.getElementById("miaoshutable").innerHTML=ret;
    return ret;
}


function delall(){
var pwd=$("input#adminpwd")[0].value;
if(pwd.length<4){
    alert("无权操作");
    return;
}
var type=deloption();
if(confirm("你确定要删除"+type+"表中所有数据？\n删除后将无法恢复!")){

var json={"admin":pwd,'type':type};
console.log(json);
//ajax_post("admin/all_delete.php",json,test);    
ajax_post("admin/delete/",json,test);    
}
}
function searchall(json){
//ajax_post("admin/search/all_search.php",json,search_ret);    
ajax_post("admin/search/",json,search_ret);    
}




function postxuanhong(phppath,adminpwd){
  for(var i in xuanhong_npclist){ //i是对象数组里的对象
            var b=Object.getOwnPropertyNames(xuanhong_npclist[i]);  //b是找到对象数组[对象]里的属性  比如xuanhong_npclist[雪亭镇]  可得到里面的npc名列表
            for(var j=0;j<b.length;j++){  //遍历npc名列表，b[j]是里面指定的npc名如农夫  xuanhong_npclist[i][b[j]]就是 xuanhong_npclist[雪亭镇][npc列表[0]]
                var path=xuanhong_walkway[i][b[j]];
                if(typeof(path)=="undefined"){
                    path="暂无";
                }
                var json={"type":"悬红","map":i,"npcname":b[j],"mykey":xuanhong_npclist[i][b[j]],"myvalue":path,"adminpwd":adminpwd};
                ajax_post(phppath,json,test);
            }        
    }    
}

function postmjmap(phppath,type,objname,adminpwd){//秘境地图单独上传函数
   var b=Object.getOwnPropertyNames(objname);
    console.log(b+"=="+b.length);
    var key="";
    var value="";
    for(var i=0;i<b.length;i++){
        key=b[i];
        value=objname[b[i]];
        if(typeof(value)=="undefined" ||value==""){
                    value="暂无";
                }
        var json={"type":type,"mykey":key,"myvalue":value[1],"map":value[0],"adminpwd":adminpwd};
        console.log(json);
        ajax_post(phppath,json,test);
    }
    
}


function postmap(phppath,adminpwd){
     for(var i in lunjian_maps){ //i是对象数组里的对象
            var b=Object.getOwnPropertyNames(lunjian_maps[i]);  //b是找到对象数组[对象]里的属性  比如lunjian_maps[雪亭镇]  可得到里面的小地图名列表
            for(var j=0;j<b.length;j++){  //遍历小地图名列表，b[j]是里面指定的小地图名如雪亭驿  lunjian_maps[i][b[j]]就是 lunjian_maps[雪亭镇][小地图名[0]]
                var path=lunjian_maps[i][b[j]];
                if(typeof(path)=="undefined"){
                    path="暂无";
                }
                var json={"type":"地图","map":i,"mykey":b[j],"myvalue":path,"adminpwd":adminpwd};
                ajax_post(phppath,json,test);
            }        
    }    
}
//考虑文件内变量与js同名
function postsql(phppath,type,objname,refunc,adminpwd){ //提交数据 响应程序路径 字符串型, 对象名 变量型, 回调函数 变量型,验证密码 变量型
    var b=Object.getOwnPropertyNames(objname);
    console.log(b+"=="+b.length);
    var key="";
    var value="";
    for(var i=0;i<b.length;i++){
        key=b[i];
        value=objname[b[i]];
        if(typeof(value)=="undefined" ||value==""){
                    value="暂无";
                }
        var json={"type":type,"mykey":key,"myvalue":value,"adminpwd":adminpwd};
        ajax_post(phppath,json,refunc);
}
}
function getloadsql(type,adminpwd){//获取提交请求  提交类型 字符串型，验证密码,字符串型
    switch(type)
{
    case "悬红":
        //postxuanhong("admin/all_upload.php",adminpwd);
        postxuanhong("admin/upload/",adminpwd);
        break;
    case "技能":
        //postsql("admin/all_upload.php",type,lunjian_skills,test,adminpwd);
        postsql("admin/upload/",type,lunjian_skills,test,adminpwd);
        break;
    case "装备":
        //postsql("admin/all_upload.php",type,lunjian_equips,test,adminpwd);
        postsql("admin/upload/",type,lunjian_equips,test,adminpwd);
        break;
    case "灯谜":
        //postsql("admin/all_upload.php",type,lunjian_dengmis,test,adminpwd);
        postsql("admin/upload/",type,lunjian_dengmis,test,adminpwd);
        break;
    case "答题":
        //postsql("admin/all_upload.php",type,lunjian_questions,test,adminpwd);
        postsql("admin/upload/",type,lunjian_questions,test,adminpwd);
        break;
    case "挖宝":
        //postsql("admin/all_upload.php",type,lunjian_wabaos,test,adminpwd);
        postsql("admin/upload/",type,lunjian_wabaos,test,adminpwd);
        break;
    case "秘境":
        //postmjmap("admin/all_upload.php",adminpwd);
        postmjmap("admin/upload/","秘境",lunjian_mjmaps,adminpwd);
        break;
    case "江湖":
        //postsql("admin/all_upload.php",type,lunjian_jhlists,test,adminpwd);
        postsql("admin/upload/",type,lunjian_jhlists,test,adminpwd);
        break;
    case "地图":
        //postmap("admin/all_upload.php",adminpwd);
        postmap("admin/upload/",adminpwd);
        break;
    case "物品":
        //postsql("admin/all_upload.php",type,lunjian_things,test,adminpwd);
        postsql("admin/upload/",type,lunjian_things,test,adminpwd);
        break;
    case "清背包":
        postsql("admin/upload/",type,lunjian_clearbags,test,adminpwd);
        break;
    case "青龙":
        postsql("admin/upload/",type,lunjian_qinglongs,test,adminpwd);
        break;
    case "隐藏npc":
        postsql("admin/upload/",type,lunjian_hidenpcs,test,adminpwd);
        break;
    case "奇侠":
        postmjmap("admin/upload/",type,lunjian_qixiainfos,adminpwd);
        break;
    default:
        alert("非法提交类型！");
}

}
function uploadsql(type){
    var adminpwd=$("input#adminpwd")[0].value;
    if(adminpwd.length<4){
    alert("无权操作");
    return;
    }
    getloadsql(type,adminpwd)
}



function txt(a){
      return a.replace(/[\ |\~|\`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\-|\_|\+|\=|\。|\||\\|\[|\]|\{|\}|\;|\:|\"|\'|\,|\<|\.|\>|\/|\?]/g,"");
    }
    
function demo(){
    getall("雪亭镇","你看到一位面色黝黑的农夫。");   
}
function getall(x,y){
    
var b=Object.getOwnPropertyNames(xuanhong_npclist[x]); 
var c=[];
var d='脚本库发现多个相同描述的npc!\n请尝试在下列npc中选择\n';
var e=0;
for(var i=0;i<b.length;i++){
	if(txt(xuanhong_npclist[x][b[i]]).indexOf(txt(y))>-1){
		c.push(x+'-'+b[i]);
	}
}    
console.log(c);
}
